package cn.holy.app.xcddt.database.dao;

import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import cn.holy.app.xcddt.domain.RecordBean;

import static androidx.room.OnConflictStrategy.REPLACE;

/**
 * Created by 69568 on 2018/2/24.
 */

@Dao
public interface RecordDao {

    @Insert(onConflict = REPLACE)
    void save(RecordBean recordBean);


    @Query("SELECT * FROM records WHERE record_num = :record and user_id = :user_id order by created_at desc")
    LiveData<List<RecordBean>> queryByRecordNumber(int record, int user_id);


    @Query("SELECT count(*) FROM records WHERE record_num = :record and user_id = :user_id order by created_at desc")
    Long countByRecordNumber(int record, int user_id);

    @Query("SELECT * FROM records where user_id = :user_id order by record_num desc")
    LiveData<List<RecordBean>> queryAll(int user_id);

    @Update
    void update(RecordBean recordBean);

    @Query("delete from records where id in (:ids)")
    void deleteRecords(List<Integer> ids);

    @Delete
    void deleteRecord(RecordBean recordBean);

    @Query("SELECT * FROM records WHERE record_no = :record_no limit 1")
    LiveData<RecordBean> queryByRecordNo(@NotNull String record_no);

    @Query("SELECT * FROM records WHERE id = :id limit 1")
    LiveData<RecordBean> queryById(@NotNull int id);
}
